package com.example.waimaiandroid.OrderDetailActivity;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.waimai.HttpUtil.MyPost;
import com.example.waimai.pojo.Food;
import com.example.waimai.pojo.Orders;
import com.example.waimai.util.MyThreadPool;
import com.example.waimaiandroid.R;
import com.example.waimaiandroid.billing.MyListViewForScrollView;

import java.util.ArrayList;
import java.util.List;


public class OrderDetailActivity extends AppCompatActivity {

    private Orders order; // 订单详情
    private TextView userAddr;
    private TextView userInfo;
    private TextView storeAddr;
    private TextView storeInfo;
    private TextView De_StoreName;
    private MyListViewForScrollView orderFoods;
    private TextView De_deliveryFee;
    private TextView De_orderPrice;
    private TextView De_remark;
    private TextView BottomOrderPrice;
    private Button De_back;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            // 设置参数
            userAddr.setText(order.getUserAddress().getAddress());
            userInfo.setText(order.getUserAddress().getReceiveName()+" "+order.getUserAddress().getTelephone());
            storeAddr.setText(order.getStoreAddress().getAddress());
            storeInfo.setText(order.getStoreAddress().getBottome());
            De_StoreName.setText(order.getStore().getStoreName());
            De_deliveryFee.setText(String.valueOf(order.getDeliveryFee()));
            De_orderPrice.setText(String.valueOf(order.getTotalPrice()));
            De_remark.setText(order.getRemark());
            BottomOrderPrice.setText(String.valueOf(order.getTotalPrice()));

            // =================================================
            OrderDetailAdapter adapter = new OrderDetailAdapter(order.get_foodList(),OrderDetailActivity.this);
            orderFoods.setAdapter(adapter);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_detail);
        initData();
        initView();
        initListener();
    }

    private void initData(){
        Bundle extras = getIntent().getExtras();
        order = (Orders) extras.getSerializable("order");
        String[] split = order.getFoodIdList().split(",");
        List<Food> foods = new ArrayList<>();
        MyThreadPool.doJob(new Runnable() {
            @Override
            public void run() {
                for(int i =0;i<split.length;i++){
                    String[] temp = split[i].split("-");
                    Food food = new Food();
                    food = (Food) MyPost.doPost("bossgetFood",Integer.valueOf(temp[0]));
                    food.setAddNums(Integer.valueOf(temp[1]));
                    foods.add(food);
                }
                order.set_foodList(foods);
                // 跳转到详细页面
                Message msg = new Message();
                handler.sendMessage(msg);
            }
        });
        System.out.println(order);
    }

    private void initView(){
          userAddr = findViewById(R.id.userAddr);
          userInfo= findViewById(R.id.userInfo);
          storeAddr= findViewById(R.id.storeAddr);
          storeInfo= findViewById(R.id.storeInfo);
          De_StoreName= findViewById(R.id.De_storeName);
          orderFoods= findViewById(R.id.orderFoods);
          De_deliveryFee= findViewById(R.id.De_deliveryFee);
          De_orderPrice= findViewById(R.id.De_orderPrice);
          De_remark= findViewById(R.id.De_remark);
          BottomOrderPrice= findViewById(R.id.BottomOrderPrice);
          De_back= findViewById(R.id.De_back);



        /*// 设置监听器
        De_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(com.example.boos.orderDetail.OrderDetailActivity.this, MainActivity.class));
                com.example.boos.orderDetail.OrderDetailActivity.this.startActivity(intent);
            }
        });*/
    }

    private void initListener(){
        De_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                finish();
            }
        });
    }
}